;solution for euler 64
;references:
;http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Continued_fraction_expansion

(defun square (a) (* a a))

(defun find-period (S)
	(let ((m 0) (d 1) (a0 (floor (sqrt S))) a m1 d1 a1 (period 0))
		(when (= a0 (sqrt S)) (return-from find-period 0))
		(setf a a0)
		(loop
			(setf m (- (* d a) m))
			(setf d (/ (- S (* m m)) d))
			(setf a (floor (/ (+ a0 m) d)))
			(when (= period 0) 
				(setf m1 m)
				(setf d1 d)
				(setf a1 a))
			(when (and (/= period 0) (= a a1) (= m m1) (= d d1))
				(return-from find-period period))
			(incf period))))
			
(defun solve-euler64 ()
	(let ((count 0) res)
		(dotimes (i 10001)
			(setf res (find-period i))
				(if (oddp res) (incf count)))
		(format t "Euler 64:~a" count)))